package ggze.GGZEServer.home.service.impl;

import ggze.GGZEServer.home.dao.inter.SoBlanceDao;
import ggze.GGZEServer.home.dao.inter.SoBlanceRecordDao;
import ggze.GGZEServer.home.entity.SoBlance;
import ggze.GGZEServer.home.entity.SoBlanceRecord;
import ggze.GGZEServer.home.enums.BlanceEnum;
import ggze.GGZEServer.home.service.BlanceService;
import ggze.GGZEServer.home.service.ConsumeService;
import ggze.GGZEServer.home.service.ConvertService;
import ggze.actuator.ActuatorController;
import ggze.esp.entity.FspUser;
import ggze.tool.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class BlanceServiceImpl implements BlanceService {
    @Autowired
    private ActuatorController actuatorController;
    @Autowired
    private SoBlanceDao soBlanceDao;
    @Autowired
    private SoBlanceRecordDao soBlanceRecordDao;
    @Autowired
    private ConsumeService consumeService;
    @Autowired
    private ConvertService convertService;

    @Override
    public boolean createBlance(FspUser user) {
        try {
            SoBlance soBlance = soBlanceDao.isSelect("f_user_id", user.getUuid());
            if (soBlance == null) {
                soBlance = new SoBlance();
                soBlance.setUuid("");
                soBlance.setF_BLANCE(0.0);
                soBlance.setF_CONSUME_JF(0.0);
                soBlance.setF_CONVERT_JF(0.0);
                soBlance.setF_INCOME_JE(0.0);
                soBlance.setF_EXPEND_JE(0.0);
                soBlance.setF_USER_ID(user.getUuid());
                soBlance.setF_USER_NAME(user.getF_NICKNAME());
                soBlance.setF_CREATE_TIME(TimeUtils.getTimeFormat());
                soBlance.setF_METHOD_TIME(TimeUtils.getTimeFormat());
                actuatorController.execute(soBlance, "add", soBlanceDao);
            }
            return true;
        } catch (Exception e) {

            return false;
        }
    }

    @Transactional
    @Override
    public boolean addBlance(SoBlance soBlance) {
        try {
            SoBlance soUser = soBlanceDao.isSelect("f_user_id", soBlance.getF_USER_ID());
            if (soBlance.getF_BLANCE() != 0) {
                this.changeBlance(soBlance, "0");
            }
            if (soBlance.getF_CONVERT_JF() != 0) {
                convertService.changeConvert(soBlance,"0");
            }
            if (soBlance.getF_CONSUME_JF() != 0) {
                consumeService.changeConsume(soBlance,"0");
            }
            return true;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    //余额变动
    @Override
    public boolean changeBlance(SoBlance soBlance, String type) throws Exception {
        SoBlance soUser = soBlanceDao.isSelect("f_user_id", soBlance.getF_USER_ID());
        if (soBlance.getF_BLANCE() != 0) {
            soUser.setF_BLANCE(soUser.getF_BLANCE() + soBlance.getF_BLANCE());
        }
        actuatorController.execute(soUser, "update", soBlanceDao);
        //流水
        SoBlanceRecord soBlanceRecord = new SoBlanceRecord();
        String time = TimeUtils.getTimeFormat();
        soBlanceRecord.setF_CREATE_TIME(time);
        soBlanceRecord.setF_METHOD_TIME(time);
        soBlanceRecord.setF_NOTE(BlanceEnum.BLANCE.getLables(type));
        soBlanceRecord.setF_SELES1(type);
        soBlanceRecord.setF_USER_NAME(soUser.getF_USER_NAME());
        soBlanceRecord.setF_USRE_ID(soUser.getF_USER_ID());
        soBlanceRecord.setF_MONEY(soBlance.getF_BLANCE());
        actuatorController.execute(soBlanceRecord, "add", soBlanceRecordDao);
        return true;
    }
}
